﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;

namespace Bungalow
{
   
   
    public class SPTabView : Panel
    {
        private SPTab activeTab;
        public SPTab ActiveTab
        {
            get
            {
                return activeTab;
            }
            set
            {
                this.activeTab = value;
            }
        }
        public Dictionary<String, SPTab> Views { get; set; }
        Panel contentPanel = new Panel();
        private int tabHeight = 24;
        private int scrollX = 0; // ScrollX of tabs
        public void Draw(Graphics g)
        {
            // fill
        //    g.FillRectangle(new SolidBrush(Color.FromArgb(180, 180, 180)), new Rectangle(0, 0, this.Width, this.Height));

            // fill tabBar
      //      g.FillRectangle(Program.Skin.TabBarBackgroundGradient, new Rectangle(0, 1, this.Width, this.Height-1));
       //     g.DrawLine(new Pen(Color.FromArgb(172, 172, 172), 1), new Point(0, tabHeight - 2), new Point(this.Width, tabHeight - 2));
             g.DrawImage(Properties.Resources.tabbar,new Rectangle(0,0,this.Width*25,this.Height));
 
         //   g.DrawLine(new Pen(Color.FromArgb(42,42,42), 1), new Point(0, this.Height -2), new Point(this.Width, this.Height - 2));
            // Fill views

            // Get all test
            StringBuilder sb = new StringBuilder();
            foreach(SPTab tab in Views.Values) {
                sb.Append(tab.Title) ;
            }
            int c_width = (int)g.MeasureString(sb.ToString(), new Font("MS Sans Serif", 11)).Width;
            int pos = this.Width / 2 - (int)(c_width /2.5);
            foreach (SPTab tab in Views.Values)
            {
                Color foreColor = Color.FromArgb(255, 255, 255);
                Color shadowColor = Color.FromArgb(0, 0, 0);
                int str_width = (int)(g.MeasureString(tab.Title,new Font( "MS Sans Serif", 8)).Width) ;
                if(ActiveTab != null)
                if (tab.Uri == ActiveTab.Uri)
                {
                    // fill tabBar
                    g.FillRectangle(new SolidBrush(Color.Black), new Rectangle(pos, 2, str_width + 18 ,this.Height - 6 ));
            
                    // fill active tab
                     foreColor = Color.FromArgb(211, 211, 211);
                     shadowColor = Color.FromArgb(55, 55, 55);


            //         g.DrawLine(new Pen(Color.FromArgb(122, 122, 122), 1), new Point(pos, 1), new Point(pos, this.Height - 3));
                }
                bool isSaved = false;
              
                // Draw string
                g.DrawString(tab.Title , new Font("MS Sans Serif", 8), new SolidBrush(shadowColor), new Point(pos + 6, 6));
                g.DrawString(tab.Title , new Font("MS Sans Serif", 8), new SolidBrush(foreColor), new Point(pos + 6, 5));
                if (activeTab == null || tab.Uri != ActiveTab.Uri)
                {
       //             g.DrawLine(new Pen(Color.FromArgb(172, 172, 172), 1), new Point(pos + 120, 1), new Point(pos + 120, this.Height - 4));
         //           g.DrawLine(new Pen(Color.FromArgb(122, 122, 122), 1), new Point(pos + 121, 1), new Point(pos + 121, this.Height - 4));
                } pos += str_width + 18;
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            BufferedGraphicsContext bgc = new BufferedGraphicsContext();
            BufferedGraphics bg = bgc.Allocate(e.Graphics, new Rectangle(0, 0, this.Width, this.Height));
            Graphics g = bg.Graphics;
            Draw(g);
            bg.Render();
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
            BufferedGraphicsContext bgc = new BufferedGraphicsContext();
            BufferedGraphics bg = bgc.Allocate(e.Graphics, new Rectangle(0, 0, this.Width, this.Height));
            Graphics g = bg.Graphics;
            Draw(e.Graphics);
            bg.Render();
        }
        /// <summary>
        /// Show a control
        /// </summary>
        /// <param name="control"></param>
        /// <param name="title"></param>
        /// <param name="uri"></param>
        public void ShowControl(Control control, String title, Uri uri)
        {
            String url = uri.ToString();
    
            contentPanel.Controls.Add(control);
            control.Dock = DockStyle.Fill;
            SPTab tab = new SPTab();
            tab.Control = control;
            tab.Title = title;
            tab.Uri = uri;
            this.activeTab = tab;
            Views.Add(url, tab);
            control.BringToFront();
            
        }

        void comp_Saved(object sender, EventArgs e)
        {
            Draw(CreateGraphics());
        
        }

        void comp_Changed(object sender, EventArgs e)
        {
            Draw(CreateGraphics());
         
        }
        public SPTabView()
            : base() {
                Views = new Dictionary<String, SPTab>();
                this.BackColor = ( Program.Skin.BackgroundColor);
                // Add contentpanel
                this.Controls.Add(contentPanel);
                contentPanel.Left = 0;
                contentPanel.Top = tabHeight;
                contentPanel.Width = this.Width;
                this.Resize += new EventHandler(SPTabView_Resize);
                contentPanel.Height = this.Height - tabHeight;
                contentPanel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
                this.MouseDown += new MouseEventHandler(SPTabView_MouseDown);
                this.TabStop = true;
        }

        void SPTabView_Resize(object sender, EventArgs e)
        {
            this.Draw(this.CreateGraphics());
        }
        
        void SPTabView_MouseDown(object sender, MouseEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            foreach (SPTab tab in Views.Values)
            {
                sb.Append(tab.Title);
            }
            int c_width = (int)this.CreateGraphics().MeasureString(sb.ToString(), new Font("MS Sans Serif", 11)).Width;
            int pos = this.Width / 2 - (int)(c_width / 2.5);
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                foreach (SPTab tab in this.Views.Values)
                {
                    int str_width = (int)(this.CreateGraphics().MeasureString(tab.Title,new Font( "MS Sans Serif", 11)).Width) ;
                    if (e.X >= pos && e.X <= pos + str_width)
                    {
                        TabEventArgs args = new TabEventArgs(tab);
                        if (this.TabChanged != null)
                        {
                            this.TabChanged(this, args);
                        }
                        this.activeTab = tab;
                        this.Draw(this.CreateGraphics());
                        break;
                    }
                    pos += str_width + 18;
                }
            }
            
        }
        public void Navigate(String title, Uri uri)
        {
            // Add navigation handler here
            String url = uri.ToString();
         
           
            
        }
        public class TabEventArgs
        {
            public TabEventArgs(SPTab tab)
            {
                this.Tab = tab;
            }
            public SPTab Tab { get; set; }
        }
        public delegate void TabEventHandler(object sender, TabEventArgs e);
        public event TabEventHandler TabChanged;
    }
    /// <summary>
    /// A SPTab
    /// </summary>
    public class SPTab
    {
        public String Title { get; set; }
        public Control Control { get; set; }
        public Uri Uri { get; set; }
    }
}
